home *** CD-ROM | disk | FTP | other *** search
- /*
- * hippo.c - histogramming package in C.
- *
- * Copyright (C) 1991 The Board of Trustees of The Leland Stanford
- * Junior University. All Rights Reserved.
- *
- * $Id: hippontuple.c,v 3.9 1992/04/10 22:48:01 rensing Rel $
- *
- * by jonas karlsson, at SLAC, August 1990
- * split up by Paul Rensing, Feb 28,1991
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- /* on SGI FLT_MIN, FLT_MAX are in limits.h also */
- #ifndef sgi
- #include <float.h>
- #endif
- #include <limits.h>
- #include <string.h>
- #include "hippo.h"
-
- GLOB_QUAL const char hippontuple_c_rcsid[] =
- "$Id: hippontuple.c,v 3.9 1992/04/10 22:48:01 rensing Rel $";
- GLOB_QUAL const char hippo_h_rcsid[] = HIPPO_H_RCSID;
-
- #define BLOCKSIZE 4096 /* blocksize for allocating memory for */
- /* data; units = number of points */
- #define NEW_NT_NBLOCKS 1 /* new of blocks allocated for a new ntuple */
-
- #define INDEX(rows, columns, totcols) ((rows) * (totcols) + (columns))
- #define MIN(x, y) (((x) > (y)) ? (y) : (x))
-
- ntuple h_new(int ndim)
- {
- ntuple nt;
- int i;
- char string[21];
-
- if ( (nt = (ntuple) malloc(sizeof(ntuple_t))) == NULL)
- return NULL;
-
- nt->ndim = ndim;
- nt->ndata = 0;
- nt->rev = 0;
- nt->title = NULL;
- nt->label = NULL;
- nt->nhigh = nt->nlow = NULL;
- nt->extremeBad = 0;
-
- /*
- * allocate memory for data.
- */
- if ((nt->data =
- (float *)malloc(NEW_NT_NBLOCKS*BLOCKSIZE*nt->ndim*sizeof(float)))
- == NULL)
- goto error_ret;
- nt->memAlloc = NEW_NT_NBLOCKS * BLOCKSIZE;
-
- /*
- * arrays for minimum/maximum value of data dimension n.
- */
- if ((nt->nlow = (float *) malloc(ndim * sizeof(float))) == NULL)
- goto error_ret;
- if ((nt->nhigh = (float *) malloc(ndim * sizeof(float))) == NULL)
- goto error_ret;
- for (i = 0; i < ndim; i++) {
- nt->nlow[i] = FLT_MAX;
- nt->nhigh[i] = -FLT_MAX;
- }
-
- /*
- * title of ntuple and labels for each dimension.
- */
- if ((nt->label = (char **)malloc(ndim * sizeof(char *))) == NULL)
- goto error_ret;
-
- /*
- * set title to blank string; XDR cannot handle NULL pointers as strings.
- */
- if ((nt->title = (char *)malloc( 2*sizeof(char) )) == NULL)
- goto error_ret;
- strcpy(nt->title, "");
- for (i=0; i<ndim; i++ )
- {
- if ((nt->label[i] = (char *)malloc( 20*sizeof(char) )) == NULL)
- goto error_ret;
- sprintf( string, "Item %d", i);
- strncpy( nt->label[i], string, 20 );
- }
-
- return nt;
-
- error_ret:
- h_freeNt( nt );
- return (ntuple) NULL;
- }
-
- int h_freeNt( ntuple nt )
- {
- int i;
-
- if (nt == NULL) return -1;
-
- /*
- * free memory for data.
- */
- if (nt->data != NULL) free( nt->data );
-
- /*
- * arrays for minimum/maximum value of data dimension n.
- */
- if (nt->nlow != NULL) free( nt->nlow );
- if (nt->nhigh != NULL) free( nt->nhigh );
-
- /*
- * title of ntuple and labels for each dimension.
- */
- if (nt->title != NULL) free( nt->title );
- if (nt->label != NULL)
- {
- for (i=0; i<nt->ndim; i++ )
- if (nt->label[i] != NULL) free( nt->label[i] );
- free( nt->label );
- }
-
-
- free( nt );
-
- return 0;
- }
-
- int h_ntSize( ntuple nt )
- {
- int i = 0;
- int j;
-
- if (nt == NULL) return 0;
-
- i += 4; /* magic number */
- i += (strlen(STRUCT_VERSION)/4 + 2)*4;
- i += sizeof( ntuple_t )+8;
-
- i += (strlen(nt->title)/4 + 2)*4;
- for (j=0; j<nt->ndim; j++)
- i += (strlen(nt->label[j])/4 + 2)*4;
-
- i += 2 * (nt->ndim * sizeof(float) + 4); /* nlow and nhigh */
- i += nt->ndata*nt->ndim * sizeof(float) + 4;
-
- i += 100; /* just in case */
-
- return i;
- }
-
- int h_fill(ntuple nt, ...)
- {
- va_list argPtr;
- int i;
- float q;
-
- if (nt->ndata == nt->memAlloc) {
- nt->data = (float *) realloc(nt->data,
- (nt->memAlloc + BLOCKSIZE)
- * nt->ndim * sizeof(float));
- if (nt->data == NULL) {
- return -1;
- }
-
- nt->memAlloc += BLOCKSIZE;
- }
-
- va_start(argPtr, nt);
-
- for (i = 0; i < nt->ndim; i++) {
- q = nt->data[INDEX(nt->ndata, i, nt->ndim)] =
- va_arg(argPtr, double);
-
- if (q < nt->nlow[i]) {
- nt->nlow[i] = q;
- }
-
- if (q > nt->nhigh[i]) {
- nt->nhigh[i] = q;
- }
-
- }
-
- nt->ndata++;
-
- va_end(argPtr);
-
- return 0;
- }
-
- int h_arrayFill(ntuple nt, float *data )
- {
- int i;
- float q;
-
- if (nt->ndata == nt->memAlloc) {
- nt->data = (float *) realloc(nt->data,
- (nt->memAlloc + BLOCKSIZE)
- * nt->ndim * sizeof(float));
- if (nt->data == NULL) {
- return -1;
- }
-
- nt->memAlloc += BLOCKSIZE;
- }
-
- for (i = 0; i < nt->ndim; i++) {
- q = nt->data[INDEX(nt->ndata, i, nt->ndim)] = data[i];
-
- if (q < nt->nlow[i]) {
- nt->nlow[i] = q;
- }
-
- if (q > nt->nhigh[i]) {
- nt->nhigh[i] = q;
- }
-
- }
-
- nt->ndata++;
-
- return 0;
- }
-
- int h_setNtTitle( ntuple nt, const char *title )
- {
- int l = strlen(title);
-
- /*
- * allocate space for title
- */
- if (nt->title != NULL) free( nt->title );
- if ((nt->title = (char *) malloc((l+1) * sizeof(char))) == NULL)
- {
- return -1;
- }
-
- strcpy(nt->title, title);
-
- return 0;
- }
-
- int h_setNtLabel( ntuple nt, int dim, const char *label )
- {
- int l = strlen(label);
-
- /* allocate space for label */
- if (nt->label[dim] != NULL) free( nt->label[dim] );
- if ((nt->label[dim] = (char *) malloc( (l+1) * sizeof(char))) == NULL)
- {
- return -1;
- }
-
- strcpy(nt->label[dim], label);
-
- return 0;
- }
-
- int h_setAllNtLabels( ntuple nt, const char *label[] )
- {
- int i,l;
-
- for (i=0; i<nt->ndim; i++)
- {
- l = strlen(label[i]);
-
- /* allocate space for label */
- if (nt->label[i] != NULL) free( nt->label[i] );
- if ((nt->label[i] = (char *) malloc( (l+1) * sizeof(char))) == NULL)
- {
- return -1;
- }
-
- strcpy(nt->label[i], label[i]);
- }
-
- return 0;
- }
-
-
- int h_clrNt( ntuple nt )
- {
- int i;
-
- nt->ndata = 0;
-
- /*
- * allocate memory for data.
- */
- if ((nt->data =
- (float *)realloc(nt->data,
- NEW_NT_NBLOCKS*BLOCKSIZE*nt->ndim*sizeof(float)))
- == NULL)
- {
- return -1;
- }
- nt->memAlloc = NEW_NT_NBLOCKS * BLOCKSIZE;
-
- for (i = 0; i < nt->ndim; i++)
- {
- nt->nlow[i] = FLT_MAX;
- nt->nhigh[i] = -FLT_MAX;
- }
- nt->rev++;
-
- return 0;
- }
-
-
- const char *h_getNtLabel( ntuple nt, int dim )
- {
- if (nt != NULL && dim>=0 && dim < nt->ndim) return nt->label[dim];
- return NULL;
- }
-
- const float *h_getNtData( ntuple nt, int i_nt )
- {
- if (nt == NULL) return NULL;
- if (i_nt >= nt->ndata) return NULL;
-
- return &( nt->data[INDEX(i_nt,0,nt->ndim)] );
- }
-
- float *h_getNtColumn( ntuple nt, int dim )
- {
- float *col, *d, *cd;
- float *last;
-
- if (nt == NULL) return NULL;
- if (dim < 0 || dim >= nt->ndim) return NULL;
-
- /*
- * allocate array.
- */
- if ( (col = (float *) malloc( nt->ndata * sizeof(float) )) == NULL)
- return NULL;
-
- last = &( nt->data[INDEX(nt->ndata,0,nt->ndim)] );
- for (d = &(nt->data[INDEX(0,dim,nt->ndim)]), cd = col;
- d < last;
- d += nt->ndim, cd++)
- {
- *cd = *d;
- }
-
- return col;
- }
-
-
- int h_replData( ntuple nt, int i_nt, ... )
- {
- va_list argPtr;
- int i;
- float q;
-
- if (i_nt >= nt->ndata) return -1;
-
- va_start(argPtr, i_nt);
-
- for (i = 0; i < nt->ndim; i++)
- {
- q = nt->data[INDEX(i_nt, i, nt->ndim)] =
- va_arg(argPtr, double);
-
- if (q < nt->nlow[i]) nt->nlow[i] = q;
- if (q > nt->nhigh[i]) nt->nhigh[i] = q;
- }
-
- va_end(argPtr);
-
- nt->rev++;
- if (nt->rev > INT_MAX-100) nt->rev = 0;
-
- nt->extremeBad = 1;
-
- return 0;
- }
-
- int h_arrayReplData( ntuple nt, int i_nt, float *data )
- {
- int i;
- float q;
-
- if (i_nt >= nt->ndata) return -1;
-
- for (i = 0; i < nt->ndim; i++)
- {
- q = nt->data[INDEX(i_nt, i, nt->ndim)] = data[i];
-
- if (q < nt->nlow[i]) nt->nlow[i] = q;
- if (q > nt->nhigh[i]) nt->nhigh[i] = q;
- }
-
- nt->rev++;
- if (nt->rev > INT_MAX-100) nt->rev = 0;
-
- nt->extremeBad = 1;
-
- return 0;
- }
-